package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.SHCategory;
import com.qian.api.ddj.service.ISHCategoryService;
import com.qian.service.ddj.dao.SHCategoryRepository;
import com.qian.service.ddj.entity.DdjSHCategory;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SHCategoryServiceImpl extends BaseServiceImpl<SHCategory,DdjSHCategory,Integer> implements ISHCategoryService{
    @Autowired
    private SHCategoryRepository dao;
    @Override
    public SHCategory saveOrUpdate(SHCategory shCategory) {
        DdjSHCategory ddjSHCategory = new DdjSHCategory();
        if (shCategory.isNew()){
            dao.save(model2Entity(shCategory));
        }else{
            ddjSHCategory = dao.findOne(shCategory.getId());
            if (isNotEmpty(shCategory.getSHName())){
                ddjSHCategory.setSHName(shCategory.getSHName());
            }
            if (isNotEmpty(shCategory.getCategoryName())){
                ddjSHCategory.setCategoryName(shCategory.getCategoryName());
            }
            if (isNotEmpty(shCategory.getSercurityKey())){
                ddjSHCategory.setSercurityKey(shCategory.getSercurityKey());
            }
            shCategory = entity2model(dao.save(ddjSHCategory));
        }
        return shCategory;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjSHCategory model2Entity(SHCategory model) {
        DdjSHCategory ddjSHCategory = null;
        if (isNotEmpty(model)){
            ddjSHCategory = new DdjSHCategory();
            ddjSHCategory.setCategoryName(model.getCategoryName());
            ddjSHCategory.setId(model.getId());
            ddjSHCategory.setSercurityKey(model.getSercurityKey());
            ddjSHCategory.setSHName(model.getSHName());
        }
        return ddjSHCategory;
    }

    @Override
    protected SHCategory entity2model(DdjSHCategory entity) {
        SHCategory shCategory = null;
        if (isNotEmpty(entity)){
            shCategory = new SHCategory();
            shCategory.setCategoryName(entity.getCategoryName());
            shCategory.setId(entity.getId());
            shCategory.setSercurityKey(entity.getSercurityKey());
            shCategory.setSHName(entity.getSHName());
        }

        return shCategory;
    }
}
